#include "interface.h"
#include <gr_graph_fun.h>
#include <LOGR_functions.h>
#include <w3ld.h>

static GnomeUIInfo file4_menu_uiinfo[] =
{
  GNOMEUIINFO_MENU_CLOSE_ITEM (gtk_widget_destroy, NULL),
  //GNOMEUIINFO_MENU_EXIT_ITEM (gtk_main_quit, NULL),
  GNOMEUIINFO_END
};

static GnomeUIInfo edit4_menu_uiinfo[] =
{
  GNOMEUIINFO_MENU_CLEAR_ITEM (daemonClearDebugWindow, NULL),
  GNOMEUIINFO_END
};

static GnomeUIInfo w3live2_menu_uiinfo[] =
{
  {
    GNOME_APP_UI_ITEM, "Stop Daemon",
    NULL,
    (gpointer) daemonStop_file, NULL, NULL,
    GNOME_APP_PIXMAP_STOCK, "gtk-stop",
    0, (GdkModifierType) 0, NULL
  },
  GNOMEUIINFO_END
};

static GnomeUIInfo help4_menu_uiinfo[] =
{
  //GNOMEUIINFO_HELP ("w3live"),
  GNOMEUIINFO_MENU_ABOUT_ITEM (showAbout, NULL),
  GNOMEUIINFO_END
};

static GnomeUIInfo menubar4_uiinfo[] =
{
  GNOMEUIINFO_MENU_FILE_TREE (file4_menu_uiinfo),
  GNOMEUIINFO_MENU_EDIT_TREE (edit4_menu_uiinfo),
/*
  {
    GNOME_APP_UI_SUBTREE, "_W3Live",
    NULL,
    w3live2_menu_uiinfo, NULL, NULL,
    GNOME_APP_PIXMAP_NONE, NULL,
    0, (GdkModifierType) 0, NULL
  },
*/
  GNOMEUIINFO_MENU_HELP_TREE (help4_menu_uiinfo),
  GNOMEUIINFO_END
};

GtkWidget*
create_daemon (char *base,char *prefix,char *domain,char *port,char *log)
{
	GtkWidget *daemon;
	GtkWidget *bonobodock4;
	GtkWidget *vbox17;
	GtkWidget *scrolledwindow8;
	GtkWidget *textviewDaemon;
	GtkWidget *appbarDaemon;

	void **arg;
	pthread_t tid;

	daemon = gnome_app_new ("W3live", "w3live - daemon");
	gtk_window_set_default_size (GTK_WINDOW (daemon), 400, 300);
	gtk_window_set_destroy_with_parent (GTK_WINDOW (daemon), TRUE);

	bonobodock4 = GNOME_APP (daemon)->dock;
	gtk_widget_show (bonobodock4);

	gnome_app_create_menus (GNOME_APP (daemon), menubar4_uiinfo);

	vbox17 = gtk_vbox_new (FALSE, 0);
	gtk_widget_show (vbox17);
	gnome_app_set_contents (GNOME_APP (daemon), vbox17);

	scrolledwindow8 = gtk_scrolled_window_new (NULL, NULL);
	gtk_widget_show (scrolledwindow8);
	gtk_box_pack_start (GTK_BOX (vbox17), scrolledwindow8, TRUE, TRUE, 0);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwindow8), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

	textviewDaemon = gtk_text_view_new ();
	gtk_widget_show (textviewDaemon);
	gtk_container_add (GTK_CONTAINER (scrolledwindow8), textviewDaemon);
	gtk_text_view_set_editable (GTK_TEXT_VIEW (textviewDaemon), FALSE);
	gtk_text_view_set_cursor_visible (GTK_TEXT_VIEW (textviewDaemon), FALSE);
	gtk_text_view_set_pixels_inside_wrap (GTK_TEXT_VIEW (textviewDaemon), 5);
	gtk_text_view_set_left_margin (GTK_TEXT_VIEW (textviewDaemon), 10);
	gtk_text_view_set_right_margin (GTK_TEXT_VIEW (textviewDaemon), 10);

	appbarDaemon = gnome_appbar_new (FALSE, TRUE, GNOME_PREFERENCES_NEVER);
	gtk_widget_show (appbarDaemon);
	gnome_app_set_statusbar (GNOME_APP (daemon), appbarDaemon);

	gtk_signal_connect (GTK_OBJECT (daemon), "destroy",
                  	 GTK_SIGNAL_FUNC (gtk_widget_destroy),
                  	 NULL);
	gnome_app_install_menu_hints (GNOME_APP (daemon), menubar4_uiinfo);

	/* Store pointers to all widgets, for use by lookup_widget(). */
	GLADE_HOOKUP_OBJECT_NO_REF (daemon, daemon, "daemon");
	GLADE_HOOKUP_OBJECT (daemon, bonobodock4, "bonobodock4");
	GLADE_HOOKUP_OBJECT (daemon, menubar4_uiinfo[0].widget, "file4");
	GLADE_HOOKUP_OBJECT (daemon, file4_menu_uiinfo[0].widget, "close4");
	GLADE_HOOKUP_OBJECT (daemon, menubar4_uiinfo[3].widget, "help4");
	GLADE_HOOKUP_OBJECT (daemon, help4_menu_uiinfo[1].widget, "about6");
	GLADE_HOOKUP_OBJECT (daemon, vbox17, "vbox17");
	GLADE_HOOKUP_OBJECT (daemon, scrolledwindow8, "scrolledwindow8");
	GLADE_HOOKUP_OBJECT (daemon, textviewDaemon, "textviewDaemon");
	GLADE_HOOKUP_OBJECT (daemon, appbarDaemon, "appbarDaemon");

	file4_menu_uiinfo[0].user_data = (void *)daemon;
	edit4_menu_uiinfo[0].user_data = (void *)daemon;

	if(!(arg = (void **)malloc(sizeof(void *)*6)))
	  perror_exit("%s %d: ",__FILE__,__LINE__);

	arg[0] = (void *)textviewDaemon;
	arg[1] = (void *)strdup(base);
	arg[2] = (void *)strdup(domain);
	arg[3] = (void *)strdup(prefix);
	arg[4] = (void *)strdup(port);
	arg[5] = (void *)strdup(log);

	if(pthread_create(&tid,NULL,(w3ld),(void *)arg))
	 perror_exit("pthread_create (%s:%d)",__FILE__,__LINE__); 
	return daemon;
}

void
daemonClearDebugWindow                 (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	return;
/*
	GtkTextBuffer *buffer = gtk_text_view_get_buffer((GtkTextView *)user_data);
	gtk_text_buffer_set_text(buffer,"",-1);
*/
}


void
daemonStop                             (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{

}


void
daemonStop_file                        (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{

}
